package com.zhiyizhen.callnumber.common;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestListener;

import java.io.File;

@GlideModule
public final class ImageLoader extends AppGlideModule {
    
    private static final String CACHE_PATH = "/call_number";
    
    public static void load(Context context, String url, ImageView view) {
        load(context, url, view, null);
    }
    
    public static void load(Context context, String url, ImageView view, Option option) {
        load(context, url, view, option, null);
    }
    
    public static void load(Context context, String url, ImageView view, Option option, RequestListener<Drawable> listener) {
        if (context == null) {
            return;
        }
        if (TextUtils.isEmpty(url)) {
            if (option != null && option.getPlaceholder() > 0) {
                view.setImageResource(option.getPlaceholder());
            }
            return;
        }
    
        RequestBuilder<Drawable> request = Glide.with(context).load(url);
        if (option != null) {
            if (option.getPlaceholder() > 0) {
                request = request.placeholder(option.getPlaceholder());
            }
            if (option.getError() > 0) {
                request = request.error(option.getError());
            }
            if (option.isCenterCrop()) {
                request = request.centerCrop();
            }
            if (option.isCircleCrop()) {
                request = request.circleCrop();
            }
        }
        if (listener != null) {
            request = request.listener(listener);
        }
        request.into(view);
    }
    
    public static void load(Context context, int resourceId, ImageView imageView) {
        Glide.with(context)
                .load(resourceId)
                .into(imageView);
    }
    
    public static void clear(Context context, ImageView view) {
        if (context == null || view == null) {
            return;
        }
        Glide.with(context).clear(view);
    }
    
    public static final class Option {
        
        private int placeholder = -1;
        private int error = -1;
        private boolean centerCrop = false;
        private boolean circleCrop = false;
        
        private Option() {
        
        }
        
        public int getPlaceholder() {
            return placeholder;
        }
        
        public void setPlaceholder(int placeholder) {
            this.placeholder = placeholder;
        }
        
        public int getError() {
            return error;
        }
        
        public void setError(int error) {
            this.error = error;
        }
        
        public boolean isCenterCrop() {
            return centerCrop;
        }
        
        public void setCenterCrop(boolean centerCrop) {
            this.centerCrop = centerCrop;
        }
        
        public boolean isCircleCrop() {
            return circleCrop;
        }
        
        public void setCircleCrop(boolean circleCrop) {
            this.circleCrop = circleCrop;
        }
        
        public static Option create() {
            return new Option();
        }
    }
    
    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        int diskCacheSizeBytes = 1024 * 1024 * 100; // 100 MB
        appRootPath = new File(context.getExternalFilesDir(""), CACHE_PATH).getPath();
        builder.setDiskCache(
                new DiskLruCacheFactory(getStorageDirectory() + CACHE_PATH, diskCacheSizeBytes)
        );
    }
    
    // 外部路径
    private final String sdRootPath = Environment.getExternalStorageDirectory().getPath();
    private String appRootPath = null;
    
    private String getStorageDirectory() {
        return appRootPath;
    }

}
